package com.biz.service.basic;


import com.biz.model.Hmodel.TSysUser;
import com.biz.model.Hmodel.basic.TBaseBrand;
import com.biz.model.Pmodel.User;
import com.biz.model.Pmodel.basic.Brand;
import com.biz.model.Pmodel.basic.BrandSpeed;
import com.biz.model.Pmodel.basic.Category;
import com.biz.model.Pmodel.basic.Pbrand;
import com.biz.service.base.BaseServiceImpl;
import com.framework.dao.hdao.BaseDaoI;
import com.framework.dao.mdao.DaoSupport;
import com.framework.model.Paging;
import com.framework.model.Params;
import com.framework.utils.MD5;
import com.framework.utils.StringUtil;
import com.framework.utils.UuidUtil;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.*;

/**
 * Created by liujiajia on 2017/1/6.
 */
@Service("merchantService")
public class MerchantServiceImpl extends BaseServiceImpl<Object> implements MerchantServiceI {

    @Resource(name = "daoSupport")
    private DaoSupport dao;

    @Autowired
    private BaseDaoI baseDao;
    @Autowired
    private BaseDaoI<TBaseBrand> brandDao;

    @Autowired
    private BaseDaoI<TSysUser> sysUserDao;

    @Override
    public Paging findMerchantGrid(Params parm) {
        Paging paging= dao.findForPagings("brandMapper.findMerchantGrid",parm,"brandMapper.findMerchantCount",parm);
        return paging;
    }

    @Override
    public Brand getMerchantById(String ids) throws Exception {
        Brand brand=(Brand) dao.findForObject("brandMapper.getMerchantById",ids);
        return brand;
    }

    @Override
    public String saveOrUpdatePbrand(Pbrand pbrand) throws Exception {
        if("0".equals(pbrand.getType())){
            //新增
            TBaseBrand tBaseBrand=new TBaseBrand();
            tBaseBrand.setLogoUrl(pbrand.getLogoUrl());
            tBaseBrand.setName(pbrand.getName());
            tBaseBrand.setTelephone(pbrand.getTelephone());
            tBaseBrand.setAddress(pbrand.getAddress());
            tBaseBrand.setIntroduction(pbrand.getIntroduction());
            brandDao.save(tBaseBrand);
            TSysUser tSysUser=new TSysUser();
            tSysUser.setLoginName(pbrand.getLoginName());
            tSysUser.setPwd(MD5.md5("123456"));
            tSysUser.setIsLock((short)0);
            tSysUser.setIsdel((short) 0);
            tSysUser.setIdentity((short) 4);
            tSysUser.setIdentityCode(tBaseBrand.getBrandCode());
            tSysUser.setId(UuidUtil.get32UUID());
            tSysUser.setCreateTime(new Date());
            sysUserDao.save(tSysUser);
        }else{
            //编辑
            TBaseBrand tBaseBrand=brandDao.getById(TBaseBrand.class,pbrand.getBrandCode());
            tBaseBrand.setLogoUrl(pbrand.getLogoUrl());
            tBaseBrand.setName(pbrand.getName());
            tBaseBrand.setTelephone(pbrand.getTelephone());
            tBaseBrand.setAddress(pbrand.getAddress());
            tBaseBrand.setIntroduction(pbrand.getIntroduction());
            brandDao.update(tBaseBrand);
            String userId=(String)dao.findForObject("brandMapper.getUserId",pbrand.getBrandCode());
            TSysUser tSysUser=sysUserDao.getById(TSysUser.class,userId);
            tSysUser.setLoginName(pbrand.getLoginName());
            sysUserDao.update(tSysUser);
        }
        return  "success";
    }

    @Override
    public Pbrand getPbranById(String id) throws Exception {
        return (Pbrand)dao.findForObject("brandMapper.getMerchantById",id);
    }

    @Override
    public String delGridById(String ids) throws Exception {
        dao.update("brandMapper.delGridById",StringUtil.formatSqlIn(ids));
        dao.update("brandMapper.delTsysUser",StringUtil.formatSqlIn(ids));
        return "操作成功！";
    }

    @Override
    public int checkLoginName(String loginName) throws Exception {
        return (int)dao.findForObject("brandMapper.checkLoginName",loginName);
    }
}
